package com.hlg.xboot.web.controller;

import com.hlg.xboot.exceptions.ParameterException;
import com.hlg.xboot.model.AbstractModel;
import com.hlg.xboot.service.BaseService;
import com.hlg.xboot.web.view.ExportExcelView;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 后台管理Rest接口基类
 * Created by yangxin on 2017/7/15.
 */
public abstract class RestBaseModulesController<E extends AbstractModel, S extends BaseService>  extends RestBaseController<E,S> {

    @GetMapping("/export")
    public ModelAndView export(E e, Pageable pageable, HttpServletRequest request, HttpServletResponse response) throws Exception {
        if (service.getExportHeader() == null) {
            throw new ParameterException("不支持数据导出");
        }
        ModelAndView mav = new ModelAndView(new ExportExcelView());
        mav.addObject("fileName", service.getExportFileName());
        mav.addObject("header", service.getExportHeader());
        mav.addObject("rows", service.getExportRows(e, pageable));
        return mav;
    }

}
